home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / NextAnswers / 1168_HashTable_usage.rtf < prev    next >
Text File  |  1995-06-12  |  3KB  |  97 lines

  1. {\rtf0\ansi{\fonttbl\f0\fnil Times-Roman;\f1\fmodern Courier;\f2\fmodern Ohlfs;}
  2. \paperw13060
  3. \paperh8500
  4. \margl120
  5. \margr120
  6. {\colortbl;\red0\green0\blue0;\red84\green84\blue84;}
  7. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\b0\i0\ulnone\fs28\fc0\cf0 Q:  
  8. \fc1\cf1 How do I use the arguments to the various HashTable methods? I don't understand the mechanics of HashTable storage for my data.\
  9.  
  10. \i \
  11.  
  12. \i0 A:  The following code snippet shows how the arguments to the various HashTable methods should be used.  Note that even though data can be stored in the hash table as 
  13. \b int
  14. \b0 , 
  15. \b char *
  16. \b0 , 
  17. \b  id
  18. \b0 ,  
  19. \b void *
  20. \b0 , or any other 32-bit quantity that can be described by a type string,  the arguments of the hash table methods, such as\
  21.  
  22. \b insertKey:value:
  23. \b0  , 
  24. \b nextState:key:value
  25. \b0 ,  
  26. \b valueForKey:
  27. \b0  have to be cast to void * or const void * respectively.  See the documentation on HashTable for more details. \
  28. \
  29.  
  30. \f1\fs24     static char *month_name[]=\{\
  31.         "january", "february", "march",\
  32.         "april", "may", "june",\
  33.         "july", "august", "september",\
  34.         "october", "november", "december" \};\
  35.  
  36. \f0\fs28 \
  37.  
  38. \f1\fs24     - hashTableTest:sender\
  39.     \{\
  40.         NXHashState state;    \
  41.         const void *key;\
  42.         void *val;\
  43.         id table;\
  44.     \
  45.         unsigned count = 0;\
  46.     \
  47.     \
  48.         /* Allocate a hash table with the capacity of 12 entries. The Key\
  49.          * type is char *, the Value type is int.\
  50.          */\
  51.     \
  52.         table =[[HashTable alloc] initKeyDesc:"*" valueDesc:"i" capacity:12];\
  53.     \
  54.         /* Insert data into the table. Note that count ranges from 0 to 11 */\
  55.         while ( count < 12 )\
  56.             \{\
  57.             [table insertKey:(const void *)month_name[count] value:(void *)count];\
  58.             count++;\
  59.             \}\
  60.         \
  61.         /* Retrieve the key/value pairs from the table\
  62.          * and display them.\
  63.          * Note that you can only enumerate entries in the table once it has been\
  64.          * initialized with the insertKey:value: method.\
  65.          */\
  66.         state = [table initState];\
  67.     \
  68.         while ( [table nextState:&state key:(void *)&key value:(void *)&val] )\
  69.             \{\
  70.             fprintf(stderr, "key %s value %d\\n", key, val);\
  71.             \}\
  72.     \
  73.         /* Retrieve a particular value from the table\
  74.          * knowing its key\
  75.          */\
  76.         if ( [table isKey:(const void *)"may"] )    \
  77.             val = [table valueForKey: (void*)"may"];\
  78.         \
  79.         \
  80.         \
  81.         return self;\
  82.     \}\
  83.  
  84. \pard\tx1140\tx2300\tx3440\tx4600\tx5760\tx6900\tx8060\tx9200\tx10360\tx11520\fc1\cf1 \
  85.  
  86. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\fs28\fc1\cf1 \
  87.  Valid for 1.0, 2.0, 3.0\
  88. N.B. (The Release 2 method 
  89. \b initKeyDesc:valueDesc: capacity
  90. \b0  should be replaced by the Release 1 method 
  91. \b newKeyDesc:valueDesc:capacity
  92. \b0 ).\
  93. \
  94. QA714\
  95. \
  96.  
  97.